<template>
  <div>
     <Row>
        <form enctype="multipart/form-data">
              <input type="file" icon="ios-download-outline" @change="onFileChange">
              <Button type="ghost" icon="ios-cloud-upload-outline" @click="onImport(this)" >导入全表Excel</Button>
        </form>

        <input v-model="gameId" placeholder="gameId" ></input>
        <Button type="primary" @click="onSearch">查询</Button>
        <Button type="primary" size="large" @click="onExport"  icon="ios-download-outline"></Icon>导出全表EXCEL</Button>
    </Row>
    <Table ref="table" class="bottom-table" :border="true" :stripe="true" :show-header="true" :data="tableData" :columns="tableColumns" ></Table>
    <Page class="bottom-page" :total="totalPage" :current="crtPage" :page-size="pageSize" show-total show-elevator @on-change="onChange"></Page>
    <Modal
        title="编辑SymbolsProbability"
        v-model="editSymbolsProbabilityModal" ok-text="提交" @on-ok="onSubmit">
      <Form  label-position="left" :model="formEdit" :label-width="100">
        <Form-item label="gameId">
            <Input v-model="formEdit.gameId" disabled ></Input>
        </Form-item>
        <Form-item label="status">
            <Input v-model="formEdit.status" ></Input>
        </Form-item>
        <Form-item label="element1">
            <Input v-model="formEdit.element1" disabled></Input>
        </Form-item>
        <Form-item label="element1Rate">
            <Input v-model="formEdit.element1Rate"></Input>
        </Form-item>
        <Form-item label="element2">
            <Input v-model="formEdit.element2" disabled></Input>
        </Form-item>
        <Form-item label="element2Rate">
            <Input v-model="formEdit.element2Rate"></Input>
        </Form-item>
        <Form-item label="element3">
            <Input v-model="formEdit.element3" disabled></Input>
        </Form-item>
        <Form-item label="element3Rate">
            <Input v-model="formEdit.element3Rate"></Input>
        </Form-item>
        <Form-item label="element4">
            <Input v-model="formEdit.element4" disabled></Input>
        </Form-item>
        <Form-item label="element4Rate">
            <Input v-model="formEdit.element4Rate"></Input>
        </Form-item>
        <Form-item label="element5">
            <Input v-model="formEdit.element5" disabled></Input>
        </Form-item>
        <Form-item label="element5Rate">
            <Input v-model="formEdit.element5Rate"></Input>
        </Form-item>
      </Form>
    </Modal>
  </div>
</template>
<script>

import {getSymbolsProbabilities, getAllSymbolsProbabilities, modifySymbolsProbability, importSymbolsProbabilities} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
import {exportExcel, importExcel} from '@/common/excelUtil.js'
export default {
  data () {
    return {
      tableData: [],
      editSymbolsProbabilityModal: false,
      results: '',
      gameId: '',
      crtPage: 1,
      totalPage: 0,
      pageSize: 10,
      filename: 'symbols_probability',
      formEdit: {
        gameId: '',
        status: '',
        element1: '',
        element1Rate: '',
        element2: '',
        element2Rate: '',
        element3: '',
        element3Rate: '',
        element4: '',
        element4Rate: '',
        element5: '',
        element5Rate: ''
      }
    }
  },
  created () {
    this.onSearch()
  },
  methods: {
    onSearch () {
      let tGameId = -1
      if (this.gameId !== '') {
        tGameId = parseInt(this.gameId)
      }

      console.log('tGameId:', tGameId, ',crtPage:', this.crtPage, ',pageSize:', this.pageSize)
      getSymbolsProbabilities(this.crtPage, this.pageSize, tGameId).then((data) => {
        if (!data) {
          return
        }

        this.tableData = []
        let SymbolsProbabilityVO = getVO('SymbolsProbabilityVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = SymbolsProbabilityVO.decode(pv.data[i])
          vo.gameId = vo.gameId.toNumber()
          vo.element1Rate = vo.element1Rate.toNumber()
          vo.element2Rate = vo.element2Rate.toNumber()
          vo.element3Rate = vo.element3Rate.toNumber()
          vo.element4Rate = vo.element4Rate.toNumber()
          vo.element5Rate = vo.element5Rate.toNumber()
          this.tableData.push(vo)
        }
        this.totalPage = pv.intValues[1]
        console.log(this.tableData)
      })
    },
    onChange (item) {
      console.log(item)
      this.crtPage = item
      this.onSearch()
    },
    onUploadSuccess (response, file, fileList) {
      this.$Message.info('load success!!!')
      console.log(response)
      console.log(file)
      console.log(fileList)
    },
    showEditSymbolsProbability (index) {
      let data = this.tableData[index]
      this.formEdit.gameId = data.gameId
      this.formEdit.status = data.status
      this.formEdit.element1 = data.element1
      this.formEdit.element1Rate = data.element1Rate
      this.formEdit.element2 = data.element2
      this.formEdit.element2Rate = data.element2Rate
      this.formEdit.element3 = data.element3
      this.formEdit.element3Rate = data.element3Rate
      this.formEdit.element4 = data.element4
      this.formEdit.element4Rate = data.element4Rate
      this.formEdit.element5 = data.element5
      this.formEdit.element5Rate = data.element5Rate

      this.editSymbolsProbabilityModal = true
    },
    onSubmit () {
      if (this.formEdit.gameId === '' || this.formEdit.status === '' || this.formEdit.element1 === '' || this.formEdit.element1Rate === '' || this.formEdit.element2 === '' || this.formEdit.element2Rate === '' || this.formEdit.element3 === '' || this.formEdit.element3Rate === '' || this.formEdit.element4 === '' || this.formEdit.element4Rate === '' || this.formEdit.element5 === '' || this.formEdit.element5Rate === '') {
        this.$Message.error('编辑参数不能为空!!!')
        return
      }

      let SymbolsProbabilityVO = getVO('SymbolsProbabilityVO')
      var smbolsProbabilityVO = new SymbolsProbabilityVO()
      smbolsProbabilityVO.gameId = Number(this.formEdit.gameId)
      smbolsProbabilityVO.status = parseInt(this.formEdit.status)
      smbolsProbabilityVO.element1 = this.formEdit.element1
      smbolsProbabilityVO.element1Rate = Number(this.formEdit.element1Rate)
      smbolsProbabilityVO.element2 = this.formEdit.element2
      smbolsProbabilityVO.element2Rate = Number(this.formEdit.element2Rate)
      smbolsProbabilityVO.element3 = this.formEdit.element3
      smbolsProbabilityVO.element3Rate = Number(this.formEdit.element3Rate)
      smbolsProbabilityVO.element4 = this.formEdit.element4
      smbolsProbabilityVO.element4Rate = Number(this.formEdit.element4Rate)
      smbolsProbabilityVO.element5 = this.formEdit.element5
      smbolsProbabilityVO.element5Rate = Number(this.formEdit.element5Rate)

      modifySymbolsProbability(smbolsProbabilityVO).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          let len = this.tableData.length
          for (let i = 0; i < len; i++) {
            let data = this.tableData[i]
            if (data.gameId === this.formEdit.gameId) {
              this.tableData[i].status = this.formEdit.status
              this.tableData[i].element1 = this.formEdit.element1
              this.tableData[i].element1Rate = this.formEdit.element1Rate
              this.tableData[i].element2 = this.formEdit.element2
              this.tableData[i].element2Rate = this.formEdit.element2Rate
              this.tableData[i].element3 = this.formEdit.element3
              this.tableData[i].element3Rate = this.formEdit.element3Rate
              this.tableData[i].element4 = this.formEdit.element4
              this.tableData[i].element4Rate = this.formEdit.element4Rate
              this.tableData[i].element5 = this.formEdit.element5
              this.tableData[i].element5Rate = this.formEdit.element5Rate
              break
            }
          }
        }
      })
    },
    onImport (obj) {
      // 导入EXCEL
      importSymbolsProbabilities(this.results).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          this.$Message.info('导入excel成功!!!')
          this.onSearch()
        }
      })
    },
    onExport () {
      // 导出EXCEL
      getAllSymbolsProbabilities().then((data) => {
        if (!data) {
          return
        }

        let allTableData = []
        let SymbolsProbabilityVO = getVO('SymbolsProbabilityVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = SymbolsProbabilityVO.decode(pv.data[i])
          vo.gameId = vo.gameId.toNumber()
          vo.element1Rate = vo.element1Rate.toNumber()
          vo.element2Rate = vo.element2Rate.toNumber()
          vo.element3Rate = vo.element3Rate.toNumber()
          vo.element4Rate = vo.element4Rate.toNumber()
          vo.element5Rate = vo.element5Rate.toNumber()
          allTableData.push(vo)
        }
        console.log(allTableData)
        exportExcel(this.filename, allTableData)
      })
    },
    onFileChange (e) {
      var files = e.target.files || e.dataTransfer.files
      if (!files.length) {
        return
      }
      importExcel(e.target, this.setResults)
    },
    setResults (res) {
      this.results = res
      console.log('this.results:', this.results)
    }
  },
  computed: {
    tableColumns () {
      let cols = [
        {
          title: 'gameId',
          key: 'gameId',
          align: 'center',
          fixed: 'left'
        },
        {
          title: 'status',
          key: 'status',
          align: 'center'
        },
        {
          title: 'element1',
          key: 'element1',
          align: 'center'
        },
        {
          title: 'element1Rate',
          key: 'element1Rate',
          align: 'center'
        },
        {
          title: 'element2',
          key: 'element2',
          align: 'center'
        },
        {
          title: 'element2Rate',
          key: 'element2Rate',
          align: 'center'
        },
        {
          title: 'element3',
          key: 'element3',
          align: 'center'
        },
        {
          title: 'element3Rate',
          key: 'element3Rate',
          align: 'center'
        },
        {
          title: 'element4',
          key: 'element4',
          align: 'center'
        },
        {
          title: 'element4Rate',
          key: 'element4Rate',
          align: 'center'
        },
        {
          title: 'element5',
          key: 'element5',
          align: 'center'
        },
        {
          title: 'element5Rate',
          key: 'element5Rate',
          align: 'center'
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          fixed: 'right',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: () => {
                    let idx = params.index
                    this.showEditSymbolsProbability(idx)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ]
      return cols
    }
  }
}

</script>

<style scoped>
.log-str{
  font-size: 14px;
  margin-bottom: 2px;
}
.content{
  min-height: 460px;
}
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}

.bottom-table{
  margin-top: 5px;
  margin-bottom: 10px;
}
</style>
